package com.kim.bookshop.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kim.bookshop.user.bean.User;


@WebFilter({ "/jsps/cart/*", "/jsps/order/*","/cart/*"})
public class UserFilter implements Filter {

    public UserFilter() {
        // TODO Auto-generated constructor stub
    }


	public void destroy() {
		// TODO Auto-generated method stub
	}


	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
			HttpServletRequest httpRequest=(HttpServletRequest) request;
			HttpServletResponse httpResponse=(HttpServletResponse) response;
			User user=(User) httpRequest.getSession().getAttribute("user");
			if(user!=null){
				chain.doFilter(request, response);
			}else {
				//httpRequest.setAttribute("msg", "你还没有登录");
				//httpRequest.getRequestDispatcher("/UserLogin.jsp").forward(httpRequest, response);
				httpResponse.sendRedirect("/BookShop/UserLogin.jsp");
				
			}
	}


	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
